/* 
Scenic Graphics Library
Copyright (C) 2007 Jouni Tulkki

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USApackage scenic;
*/

package scenic.show;

import java.util.Vector;

import scenic.ImageInfo;
import scenic.Image;

/**
 * This class offers an easy to use interface for playing videos. 
 * The video is not displayed directly on the screen. Instead the video
 * is written in to an image that can be used as any other image. Currently
 * this class is only implemented on Windows using DirectShow and QuickTime. 
 * On Linux the class does nothing.
 */
public class SimpleMediaFile {
	private int id;
	private Image image = new Image();
	private Vector<ImageInfo> outputFormats = new Vector<ImageInfo>();
	
	/**
	 * Creates a media player with the given media file. Use capture://
	 * as the filename to open the default video capture device.
	 * 
	 * @param filename Name of the media file.
	 */
	public SimpleMediaFile(String filename) {
		id = create(filename, image);
	}
	
	private native int create(String filename, Image image);
	private native void free(int id);
	private native void play(int id);
	private native void pause(int id);
	private native double getCurrentPosition(int id);
	private native void setCurrentPosition(int id, double position);
	private native double getLength(int id);
	private native void setOutputFormat(int id, int width, int height, int format);
	
	protected void finalize() {
		free();
	}
	
	/**
	 * Frees the resources associated with this object. This method
	 * must be called to free the resources, otherwise the resources 
	 * are not freed until the object is finalized.
	 */
	public void free() {
		if(id > 0)
		{
			free(id);
			id = 0;
		}
	}
	
	/**
	 * Returns the image that contains the video.
	 * 
	 * @return the image
	 */
	public Image getImage() {
		return image;
	}
	
	/**
	 * Starts playing the video.
	 */
	public void play() {
		play(id);
	}
	
	/**
	 * Pauses the video.
	 */
	public void pause() {
		pause(id);
	}
	
	/**
	 * Returns the current position.
	 * 
	 * @return Current position in seconds.
	 */
	public double getCurrentPosition() {
		return getCurrentPosition(id);
	}
	
	/**
	 * Sets the current position.
	 * 
	 * @param position The new position in seconds.
	 */
	public void setCurrentPosition(double position) {
		setCurrentPosition(id, position);
	}
	
	/**
	 * Returns the length of the video file.
	 * 
	 * @return Length of the video in seconds.
	 */
	public double getLength() {
		return getLength(id);
	}
	
	/**
	 * Sets the desired output format. This method only works with
	 * video capture. The settings must be supported by the capture
	 * device. Use getOutputFormats to find out the supported formats.
	 * 
	 * @param width width of the image
	 * @param height height of the image
	 * @param format format of the image
	 */
	public void setOutputFormat(int width, int height, int format) {
		setOutputFormat(id, width, height, format);
	}
	
	/**
	 * Returns the available output formats. This method works only
	 * with video capture.
	 * 
	 * @return array containing the available output formats
	 */
	public ImageInfo[] getOutputFormats() {
		return outputFormats.toArray(new ImageInfo[outputFormats.size()]);
	}
	
	/*
	 * This function is called from native code.
	 */
	@SuppressWarnings("unused")
	private void addOutputFormat(int width, int height, int format) {
		ImageInfo info = new ImageInfo();
		
		info.width = width;
		info.height = height;
		info.format = format;
		outputFormats.add(info);
		System.out.println(width + " " + height);
	}
}
